package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceimp implements StudentService {
    @Autowired
    private StudentMapper studentMapper;



    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        //跟据条件查询
        List<Student> studentList = studentMapper.page(studentQueryParam);


        Page<Student> c = (Page<Student>) studentList;

        return new PageResult<Student>(c.getTotal(), c.getResult());

    }

    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void deleteById(List<Integer> ids) {
        studentMapper.deleteById(ids);
    }

    @Override
    public Student getStuById(Integer id) {

       return  studentMapper.getStuById(id);
    }

    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }

    @Override
    public void updateVioIdandScore(short id, short score) {
       Student student = studentMapper.getStuById((int)id);
       if(student != null){
           student.setUpdateTime(LocalDateTime.now());
           student.setViolationScore(score);
           short count = student.getViolationCount();
           count++;
           student.setViolationCount(count);
           studentMapper.update(student);
       }
       return;
    }


}
